package com.ben.xinyue.seckill.validator.constraint;

import com.ben.xinyue.seckill.util.ValidatorUtil;
import com.ben.xinyue.seckill.validator.IsMobile;
import jodd.util.StringUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @ClassName: IsMobileConstraintValidator
 * @Description: 手机校验
 * @author: xinyue
 * @date: 2020/9/2 10:53 下午
 * @version: 1.0.0
 */
public class IsMobileConstraintValidator implements ConstraintValidator<IsMobile, String> {

    private boolean required = false;

    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(required || StringUtil.isNotBlank(value) ){
            return ValidatorUtil.isMobile(value);
        }
        return true;
    }
}
